import { createRouter, createWebHistory } from 'vue-router'
import { getToken } from '@/utils/cookie'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/oj-c/home'
    },
    {
      path: '/oj-c/login',
      name: 'login',
      component: () => import('@/views/Login.vue')
    },
    {
      path: '/oj-c/home',
      name: 'home',
      component: () => import('@/views/Home.vue'),
      redirect: '/oj-c/home/question',
      children: [
        {
          path: 'question',
          name: 'question',
          component: () => import('@/views/Question.vue'),
          meta: {
            showBanner: true
          }
        },
        {
          path: 'exam',
          name: 'exam',
          component: () => import('@/views/Exam.vue'),
          meta: {
            showBanner: true
          }
        },
        {
          path: 'user/exam',
          name: 'userExam',
          component: () => import('@/views/UserExam.vue'),
          meta: {
            showBanner: false
          }
        },
        {
          path: 'user/message',
          name: 'userMessage',
          component: () => import('@/views/UserMessage.vue'),
          meta: {
            showBanner: false
          }
        },
        {
          path: 'user/detail',
          name: 'userDetail',
          component: () => import('@/views/UserDetail.vue'),
          meta: {
            showBanner: false
          }
        }
      ]
    },
    {
      path: '/oj-c/anwser',
      name: 'anwser',
      component: () => import('@/views/Answer.vue')
    }
  ],
})

const whiteList = ['/oj-c/login', '/oj-c/home', '/oj-c/home/question', '/oj-c/home/exam']

router.beforeEach((to, from, next) => {
  const token = getToken()
  next()
  // if (whiteList.includes(to.path)) {
  //   if (token && to.path === '/oj-c/login') {
  //     next('/')
  //   } else {
  //     next()
  //   }
  // } else {
  //   // 不在白名单中
  //   if (token) {
  //     next()
  //   } else { // 没有登录
  //     next(`/oj-c/login?path=${to.path}`)
  //   }
  // }
})

export default router
